package com.aivin.lib_widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class ScaleImage extends ImageView {

    OnScaleCallBack onScaleCallBack;

    public ScaleImage(Context context) {
        super(context);
    }

    public ScaleImage(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ScaleImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    private float downX, downY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = event.getRawX();
                downY = event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float moveX = event.getRawX();
                float moveY = event.getRawY();

                onScaleCallBack.onScaled((int) (moveX - downX), (int) (moveY - downY));

                downX = moveX; // important
                downY = moveY;
                break;
            case MotionEvent.ACTION_UP:
                performClick();
                break;
        }
        return true;
    }


    @Override
    public boolean performClick() {
        return super.performClick();
    }

    public void setOnScaleCallBack(OnScaleCallBack onScaleCallBack) {
        this.onScaleCallBack = onScaleCallBack;
    }

    public interface OnScaleCallBack {
        void onScaled(int x, int y);
    }
}
